package zhongbo.java.air.websocet;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PubWebsocketText
 * @Description 建立连接、接收消息、关闭连接
 * @Author 红
 * @Date 2025/4/7 11:05
 */
public class PubWebsocketText extends TextWebSocketHandler {
    /**
     * 客户端连接对象缓存
     */
    Map<String, WebSocketSession> sessionCacheMap = new HashMap<>();

    public void afterConnection(WebSocketSession session)throws Exception{
        String sessionId=session.getId();
        sessionCacheMap.put(sessionId,session);
        System.out.println("建立ws的连接id:"+sessionId);
    }

   public void afterConnectionClose(WebSocketSession session, CloseStatus Status)throws Exception{
    String closseId=session.getId();
    sessionCacheMap.remove(closseId,session);
       System.out.println("ws连接断开id"+closseId);
   }


}
